/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package medico;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import connezione.PooledConnection;
import hirondelle.date4j.DateTime;
import java.sql.SQLException;
import java.util.TimeZone;
import javax.faces.context.FacesContext;
import login.BeanUser;

/**
 *
 * @author sestari
 */
public class BeanFormmod_visita {

    private String desc;
    PooledConnection conn;
    BeanMedico medico;

    /** Creates a new instance of BeanFormmod_visita */
    public BeanFormmod_visita() {
        conn = (PooledConnection) ((BeanUser) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user")).getCon();
        medico = ((BeanMedico) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("beanMedico"));
    }

    public String modifica() throws SQLException {
        Statement stmt = (Statement) conn.createStatement();
        DateTime now = DateTime.now(TimeZone.getDefault());
        String command = "UPDATE `db_user15`.`Visita`  SET "
                + " `descrizione` = \"" + getDesc() + "\""
                + " WHERE id_paziente = \"" + medico.getIdpaziente() + "\" and id_medico=\"" + medico.getIdmedico() + "\" and   CAST( now() as date) = CAST(  data_scadenza as date) ";
        stmt.executeUpdate(command);

        command = "UPDATE `db_user15`.`Visita`  SET "
                + " `richiami` = -1 "
                + " WHERE id_paziente = \"" + medico.getIdpaziente() + "\" and id_medico=\"" + medico.getIdmedico() + "\" and  CAST( now() as date) = CAST(  data_scadenza as date) ";
        stmt.executeUpdate(command);



        command = "SELECT a.e_mail, v.descrizione, v.id  FROM Visita AS v, Anagrafica AS a ";
        command += "WHERE v.id_paziente = \"" + medico.getIdpaziente() + "\" and v.id_medico=\"" + medico.getIdmedico() + "\" and    CAST( now() as date) = CAST(  v.data_scadenza as date)  AND v.id_paziente = a.id;";
        ResultSet result = (ResultSet) stmt.executeQuery(command);
        if (result.next()) {

            medico.generaPdf(medico.user.getNome() + " " + medico.user.getCognome(), String.valueOf(medico.getIdmedico()), now.format("YYYY-MM-DD"), result.getString(3), result.getString(2), result.getString(1));
        }
        return "back";
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}